package com.nf.xiaomi.service.impl;

import com.nf.mvc.util.JacksonUtils;
import com.nf.xiaomi.dao.AttributeDao;
import com.nf.xiaomi.dao.ProductDao;
import com.nf.xiaomi.dao.ProductInfoDao;
import com.nf.xiaomi.dao.impl.AttributeDaoImpl;
import com.nf.xiaomi.dao.impl.ProductDaoImpl;
import com.nf.xiaomi.dao.impl.ProductInfoDaoImpl;
import com.nf.xiaomi.model.AttributeModel;
import com.nf.xiaomi.entity.ProductInfoEntity;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.service.ProductInfoService;
import com.nf.xiaomi.service.ProductService;
import com.nf.xiaomi.util.JsonUtils;
import com.nf.xiaomi.vo.ProductInfoVO;
import com.nf.xiaomi.vo.ProductVo;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;

public class ProductInfoServiceImpl implements ProductInfoService {
    ProductInfoDao dao=new ProductInfoDaoImpl();
    Integer productAmount=0;
    @Override
    public ProductEntity getProductInfo(int pid, String memory, String color) {
        return dao.getProductInfo(pid,memory,color);
    }

    @Override
    public ProductVo getAttribute(int pid, String memory) {
        memory=memoryHand(memory);
        ProductDao productDao=new ProductDaoImpl();
        ProductEntity product = productDao.getProductByPid(pid);
        List<ProductInfoEntity> list = dao.getAttribute(pid,memory);
        List<String> colors=new ArrayList<>();
        List<String> memories=new ArrayList<>();
        for (ProductInfoEntity specification : list) {
            AttributeModel m = JsonUtils.readValue(specification.getAttributeList(), AttributeModel.class);
            colors.add(m.getColor());
            memories.add(m.getMemory());
        }
        colors=new ArrayList<>(new HashSet<>(colors));
        memories=new ArrayList<>(new HashSet<>(memories));
        product.setProductPrice(list.get(0).getPrice());

        return new ProductVo(product,colors,memories);

    }

    @Override
    public List<ProductEntity> getProductInfoList(int pid) {
        return dao.getProductInfoList(pid);
    }

    @Override
    public Integer insertProductInfo(Integer productID, List<ProductInfoVO> infos, String[] fileNames) {
        List<ProductInfoEntity> productInfoList = initProductInfoList(infos,fileNames);
        ProductService service=new ProductServiceImpl();
        service.setProductAmount(productID,productAmount);
        return dao.insertProductInfos(productID,productInfoList);
    }

    //从前端传来的内存数据会丢失"+"符号，再此处进行处理
    private String memoryHand(String memory){
        if("%%".equals(memory)){
            return memory;
        }
        String[] str = memory.split(" ");
        memory="%";
        for (int i = 0; i < str.length; i++) {
            if(i==str.length-1){
                memory+=str[i];
                break;
            }
            memory+=str[i]+"+";
        }
        return memory+"%";
    }

    //处理数据库获取的attribute
    private Map<Integer,String> getAttributeMap(){
        Map<Integer,String> attributeMap =new HashMap<>();

        AttributeDao attributeDao=new AttributeDaoImpl();
        Map<Integer, Map<String, Object>> resultMap = attributeDao.getAttributeMap();
        resultMap.forEach((k,v)->{
            String value = v.get("attribute_list").toString();
            String attribute = value.replaceAll("\\s", "");
            attributeMap.put(k,attribute);
        });
        return attributeMap;
    }

    private BigInteger insertAttribute(String attribute){
        AttributeDao dao=new AttributeDaoImpl();
        return dao.insertAttribute(attribute);
    }

    private List<ProductInfoEntity> initProductInfoList(List<ProductInfoVO> productInfos,String[] fileNames) {
        productAmount=0;
        Integer index=0;
        String color=null;
        Map<Integer, String> attributeMap = getAttributeMap();

        List<ProductInfoEntity> infoList=new ArrayList<>();
        for (ProductInfoVO infoVO : productInfos) {
            if (color != null && !color.equals(infoVO.getColor())) {
                index++;
            }
            AttributeModel attributeModel=new AttributeModel();
            attributeModel.setMemory(infoVO.getMemory());
            attributeModel.setColor(infoVO.getColor());
            color=infoVO.getColor();
            String attributeList = JacksonUtils.toJson(attributeModel);

            ProductInfoEntity info = new ProductInfoEntity(null,
                    attributeList,
                    new BigDecimal(infoVO.getPrice()),
                    Integer.parseInt(infoVO.getAmount()),
                    fileNames[index]
                    );
                    productAmount+=info.getAmount();
            for (Map.Entry<Integer, String> entry : attributeMap.entrySet()) {
                if(attributeList.equals(entry.getValue())){
                    info.setId(entry.getKey());
                    break;
                }
            }
            if(info.getId()==null || info.getId()==0){
                BigInteger attributeID = insertAttribute(attributeList);
                info.setId(attributeID.intValue());
            }
            infoList.add(info);
        }
        return infoList;
    }
}
